package com.xr.blog.service.impl;

import com.xr.blog.dao.BlogLogMapper;
import com.xr.blog.pojo.BlogLog;
import com.xr.blog.service.BlogLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;


@Service
public class BlogLogServiceImpl extends ServiceImpl<BlogLogMapper, BlogLog> implements BlogLogService {

    @Autowired
    BlogLogMapper blogLogMapper;

    @Override
    public Map<String, Integer> statBrowser() {
        Map<String,Integer> result=new HashMap<>();
        List<String> browsers=blogLogMapper.selectAllBrowser();
        browsers.forEach(browser -> result.merge(browser, 1, Integer::sum));
        return result;
    }

    @Override
    public Map<String, Integer> statOperatingSystem() {
        Map<String,Integer> result=new HashMap<>();
        List<String> operatingSystems=blogLogMapper.selectAllOperatingSystem();
        operatingSystems.forEach(operatingSystem -> result.merge(operatingSystem, 1, Integer::sum));
        return result;
    }

    @Override
    public List<Map<String, Integer>> statCity() {
        return blogLogMapper.selectCityCount();
    }

    @Override
    public List<BlogLog> findLatestLog(Integer number) {
        return blogLogMapper.selectLatestLog(number);
    }

    @Override
    @Async
    public void addLog(BlogLog log) {
        if(Objects.isNull(log.getIp()))return;
        save(log);
    }
}